package com.lxk.mall.pms.controller.app;

import cn.hutool.core.collection.CollUtil;
import com.lxk.mall.pms.controller.app.vo.AppFavoritePageReqVO;
import com.lxk.mall.pms.controller.app.vo.AppFavoriteReqVO;
import com.lxk.mall.pms.controller.app.vo.AppFavoriteRespVO;
import com.lxk.mall.pms.domain.PmsProductFavorite;
import com.lxk.mall.pms.domain.PmsProductSpu;
import com.lxk.mall.pms.domain.bo.favorite.PmsProductFavoriteBo;
import com.lxk.mall.pms.domain.vo.favorite.PmsProductFavoriteVo;
import com.lxk.mall.pms.service.IPmsProductFavoriteService;
import com.lxk.mall.pms.service.IPmsProductSpuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 商品收藏
 */
@RestController
@RequestMapping("/product/favorite")
@RequiredArgsConstructor
public class AppFavoriteController {

    private final IPmsProductFavoriteService productFavoriteService;

    private final IPmsProductSpuService productSpuService;

    /**
     * 添加商品收藏
     *
     * @param reqVO
     * @return
     */
    @PostMapping(value = "/create")
    public R<Long> createFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
        return R.ok(productFavoriteService.createFavorite(LoginHelper.getUserId(), reqVO.getSpuId()));
    }

    /**
     * 取消单个商品收藏
     * @param reqVO
     * @return
     */
    @DeleteMapping(value = "/delete")
    public R<Boolean> deleteFavorite(@RequestBody @Valid AppFavoriteReqVO reqVO) {
        productFavoriteService.deleteFavorite(LoginHelper.getUserId(), reqVO.getSpuId());
        return R.ok(Boolean.TRUE);
    }

    /**
     * 商品收藏分页
     *
     * @param reqVO
     * @return
     */
    @GetMapping(value = "/page")
    public TableDataInfo<AppFavoriteRespVO> getFavoritePage(AppFavoritePageReqVO reqVO) {
        PmsProductFavoriteBo bo = new PmsProductFavoriteBo();
        bo.setUserId(LoginHelper.getUserId());
        PageQuery page = new PageQuery();
        page.setPageNum(reqVO.getPageNo());
        page.setPageSize(reqVO.getPageSize());
        TableDataInfo<PmsProductFavoriteVo> favoritePage = productFavoriteService.queryPageList(bo, page);
        if (CollUtil.isEmpty(favoritePage.getRows())) {
            return TableDataInfo.build();
        }

        // 得到商品 spu 信息
        List<PmsProductFavoriteVo> favorites = favoritePage.getRows();
        List<Long> spuIds = favorites.stream().map(PmsProductFavoriteVo::getSpuId).collect(Collectors.toList());
        List<PmsProductSpu> spus = productSpuService.getSpuList(spuIds);

        // 转换 VO 结果
        List<AppFavoriteRespVO> resultList = new ArrayList<>(favorites.size());
        Map<Long, PmsProductSpu> spuMap = null;
        if (CollUtil.isNotEmpty(resultList)) {
            spuMap = spus.stream().collect(Collectors.toMap(PmsProductSpu::getId, v -> v));
        }
        for (PmsProductFavoriteVo favorite : favorites) {
            AppFavoriteRespVO appFavoriteRespVO = new AppFavoriteRespVO();
            BeanUtils.copyProperties(favorite, appFavoriteRespVO);
            if (CollUtil.isNotEmpty(spuMap) && spuMap.get(favorite.getSpuId()) != null) {
                PmsProductSpu spu = spuMap.get(favorite.getSpuId());
                appFavoriteRespVO.setSpuName(spu.getName());
            }
            resultList.add(appFavoriteRespVO);
        }
        TableDataInfo dataInfo = new TableDataInfo();
        BeanUtils.copyProperties(favoritePage, dataInfo);
        dataInfo.setRows(resultList);
        return dataInfo;
    }

    /**
     * 检查是否收藏过商品
     * @param reqVO
     * @return
     */
    @GetMapping(value = "/exits")
    public R<Boolean> isFavoriteExists(AppFavoriteReqVO reqVO) {
        PmsProductFavorite favorite = productFavoriteService.getFavorite(LoginHelper.getUserId(), reqVO.getSpuId());
        return R.ok(favorite != null);
    }

    /**
     * 获得商品收藏数量
     * @return
     */
    @GetMapping(value = "/get-count")
    public R<Long> getFavoriteCount() {
        return R.ok(productFavoriteService.getFavoriteCount(LoginHelper.getUserId()));
    }

}
